<?php

namespace App\Imports;

use App\Models\Resume;
use App\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Spatie\PdfToText\Pdf;
use Illuminate\Support\Facades\Storage;

use Brick\Math\BigDecimal;


class ResumeImport implements ToModel, WithStartRow
{
	use Importable;

	public function model(array $row)
    {
       if(!empty($row[2])){

       		if(strpos($row[2],"pdf")){


		       	$lastNameArr = explode("/",$row[2]);
		       	$lastName = end($lastNameArr);

		       	$tempPath = storage_path('app/public')."/".$lastName;


	       		//$res = Storage::put($tempPath, file_get_contents($row[1]));

		       	
		       	$res = $this->get_url_contents($row[2]);
		       	//$res =$this->get_url_contents($row[1]);

		       

		       	if(strlen($res) > 0){

		       		file_put_contents($tempPath, $res);

						$content = Pdf::getText($tempPath);
							$resume = Resume::create([
								'mi_id'=>$row[0],
		       			  'name'=>$row[1],
		       			  'content'=>$content,
		       		]);

		       	}	
				

       		}

       }
    }

     function get_url_contents($url) {
	    $ch = curl_init();

	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	    $result = curl_exec($ch);
	   
	    curl_close($ch);

	    return $result;
	}


	 public function startRow(): int
    {
        return 2;
    }

}
